/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhdt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class MyServletJspOrEjb {

    public void doSomething() throws Exception {
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mkyongdb");
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = ds.getConnection();
            conn.setAutoCommit(false);
            stmt = conn.prepareStatement("select * from categories");
            ResultSet res =  stmt.executeQuery();
            while (res.next()) {
                int id = res.getInt("id");
                System.out.println(id);
            }
            conn.commit();
            stmt.close();
            stmt = null;
            System.out.println("ok running");
            conn.close();
            conn = null;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception sqlex) {

                }

                stmt = null;
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception sqlex) {

                }

                conn = null;
            }
        }
    }
}
